package camt.andy.demo;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

public class EditPage extends Activity implements OnClickListener {

	private static String inputName, inputBirthday, inputStatus , bDay, bMonth, bYear;
	private static int position;
	EditText et_Name, et_Status;
	TextView tv_Bday;
	Button saveEdit, statusList, setDate;
	DatePickerDialog datePicker;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.editpage);

		et_Name = (EditText) findViewById(R.id.et_Name);
		et_Status = (EditText) findViewById(R.id.et_Status);
		
		tv_Bday = (TextView) findViewById(R.id.tv_Bday);
		saveEdit = (Button) findViewById(R.id.btn_Save);
		statusList = (Button) findViewById(R.id.btn_StatusList);
		setDate = (Button) findViewById(R.id.btn_Date);

		saveEdit.setOnClickListener(this);
		statusList.setOnClickListener(this);
		setDate.setOnClickListener(this);

		inputName = getIntent().getExtras().getString("name");
		inputBirthday = getIntent().getExtras().getString("birthday");
		inputStatus = getIntent().getExtras().getString("status");
		position = getIntent().getExtras().getInt("position");
		setupDisplay();

	}

	private void setupDisplay() {
		// TODO Auto-generated method stub
		et_Name.setText(inputName);
		tv_Bday.setText(inputBirthday);
		bDay = inputBirthday.substring(0, 2);
		bMonth = inputBirthday.substring(3, 5);
		bYear = inputBirthday.substring(6);		
		et_Status.setText(inputStatus);

	}

	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {

		case R.id.btn_Save:

			String sendName = et_Name.getText().toString();
			String sendBirthday = tv_Bday.getText().toString();
			String sendStatus = et_Status.getText().toString();

			Intent i = getIntent();
			i.putExtra("name", sendName);
			i.putExtra("birthday", sendBirthday);
			i.putExtra("status", sendStatus);
			i.putExtra("position", position);
			setResult(RESULT_OK, i);
			finish();

			break;

		case R.id.btn_StatusList:

			AlertDialog.Builder statusAlert = new AlertDialog.Builder(this);

			statusAlert.setTitle("Status");

			final String[] statusListArray = getResources().getStringArray(
					R.array.statuslist);

			statusAlert.setItems(statusListArray,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int item) {
							// Do something with the selection
							// because number of list not match

							et_Status.setText(statusListArray[item]);
						}

					});

			statusAlert.create();
			statusAlert.show();

			break;

		case R.id.btn_Date:

			int year = Integer.parseInt(bYear);
			int month = Integer.parseInt(bMonth) - 1;
			int day = Integer.parseInt(bDay);

			datePicker = new DatePickerDialog(this, dateListener, year, month, day);
			
			datePicker.setMessage("birthday");

			datePicker.show();

			break;

		}

	}
	
	private DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() {

		// when dialog box is closed, below method will be called.
		public void onDateSet(DatePicker view, int selectedYear,
				int selectedMonth, int selectedDay) {
			
			if(String.valueOf(selectedDay).length() < 2){
				bDay = "0" + String.valueOf(selectedDay);
				
			}else{
				
				bDay = String.valueOf(selectedDay);
				
			}
			selectedMonth += 1;
			
			if(String.valueOf(selectedMonth).length() < 2){
				bMonth = "0" + String.valueOf(selectedMonth);
				
			}else{
				
				bMonth = String.valueOf(selectedMonth);
				
			}
			
			 bYear = String.valueOf(selectedYear);

			 
			 inputBirthday = bDay + "/" + bMonth + "/" + bYear;
			 tv_Bday.setText(inputBirthday);
		}

	};

}
